package cn.miwu52.homeworkerp.Controller;

import androidx.appcompat.app.AppCompatActivity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;

import cn.miwu52.homeworkerp.R;
import cn.miwu52.homeworkerp.bean.User;
import cn.miwu52.homeworkerp.dao.UserDao;


public class userInfo extends AppCompatActivity {
    private SharedPreferences sharedPreferences;
    private UserDao userDao;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_info);
        sharedPreferences = getSharedPreferences("Controller.MainActivity", userInfo.MODE_PRIVATE);
        userDao = new UserDao(this);
        System.out.println(sharedPreferences.getString("userName", ""));
        User username = userDao.getUserInfoByName(sharedPreferences.getString("userName", ""));
        TextView userName = findViewById(R.id.userName);
        TextView userGrade = findViewById(R.id.userGrade);
        TextView userSumInEntrepot = findViewById(R.id.userSumInEntrepot);
        TextView userSumOutEntrepot = findViewById(R.id.userSumOutEntrepot);
        LinearLayout InEntrepot = findViewById(R.id.InEntrepot);
        LinearLayout OutEntrepot = findViewById(R.id.OutEntrepot);
        Button resetPassword = findViewById(R.id.resetPassword);

        resetPassword.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                View inflate = getLayoutInflater().inflate(R.layout.resetpassword, null);
                AlertDialog alertDialog = new AlertDialog.Builder(userInfo.this)
                        .setIcon(R.drawable.erp)
                        .setTitle("重置密码")
                        .setView(inflate)
                        .setNegativeButton("确认密码", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                EditText password_jiu = inflate.findViewById(R.id.password_jiu);
                                EditText password_xin1 = inflate.findViewById(R.id.password_xin1);
                                EditText password_xin2 = inflate.findViewById(R.id.password_xin2);
                                if (userDao.findPasswordByUserName(sharedPreferences.getString("userName", "userName")).equals(password_jiu.getText().toString())) {
                                    if (password_xin1.getText().toString().equals(password_xin2.getText().toString())) {
                                        if (userDao.updatePasswordByUserName(sharedPreferences.getString("userName", "userName"), password_xin1.getText().toString())) {
                                            Toast.makeText(getApplicationContext(), "修改成功，请重新登录", Toast.LENGTH_SHORT).show();
                                            Intent intent = new Intent(userInfo.this, MainActivity.class);
                                            sharedPreferences.edit().putBoolean("loginStatus", false).commit();
                                            startActivity(intent);
                                            finish();
                                        } else {
                                            Toast.makeText(getApplicationContext(), "修改失败", Toast.LENGTH_SHORT).show();
                                        }
                                    } else {
                                        Toast.makeText(getApplicationContext(), "修改失败", Toast.LENGTH_SHORT).show();
                                    }
                                } else {
                                    Toast.makeText(getApplicationContext(), "原密码不匹配，修改失败", Toast.LENGTH_SHORT).show();
                                }
                            }
                        })
                        .setPositiveButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {

                            }
                        }).create();
                alertDialog.show();
            }
        });

        userName.setText(username.getUserName());
        userGrade.setText(username.getGrade());
        userSumInEntrepot.setText(username.getTotalInEntrepot() + "");
        userSumOutEntrepot.setText(username.getTotalOutEntrepot() + "");
        if (username.getGrade().equals("入库")) {
            OutEntrepot.setVisibility(View.GONE);
        } else if (username.getGrade().equals("出库")) {
            InEntrepot.setVisibility(View.GONE);
        }
    }
}